package com.zhiqianbao.activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.zhiqianbao.R;
import com.zhiqianbao.adapter.ActivityAdapter;
import com.zhiqianbao.entity.ActivityList;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;
import com.zhiqianbao.view.PullToRefreshLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.trinea.android.common.util.StringUtils;

/**
 * Created by 子龙 on 2017/8/7.
 * 最新活动列表页面
 */

public class ApplyActiveFragmentNew extends LazyLoadFragment {
    public static final int DATA_FAIL=0x123;
    public static final int DATA_SCUESSFUL=0x124;
    private Gson gson = new Gson();
    private ListView listview1;
    private String activityPicUrl;
    private String activityContent;
    private static Handler mHandler = null;
    public static final int REFRESH_DETAIL_ACTIVITY = 2;
    private List<ActivityList.DataBean.CrmActivityListBean> crmActivityList=new ArrayList<>();
    private boolean isNewData = false;
    private ActivityAdapter activityAdapter;
    private int type;
    private View view1;
    private int pageNum=1;
    private int totalCount;
    private boolean loading  = false;
    private PullToRefreshLayout mToRefreshLayout;
    private TextView textview_data_null;
    private boolean aafn = true;
    //刷新的时候,0是刷新,1加载更多
    private int retype = 0;
    public static Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case REFRESH_DETAIL_ACTIVITY:
                    mHandler.sendEmptyMessage(0);
                    break;
            }
        }

        ;
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view1 = inflater.inflate(R.layout.activity_active_apply_tab_new, null);
        initView();
        return view1;
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_active_apply_tab_new;
    }

    @Override
    protected void lazyLoad() {
//        initData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser){
            initData();
//            showShortToast1("执行了------------------>setUserVisibleHint;");
        }
    }

    public void initData(){
        retype = 0;
        pageNum = 1;
        getDateFormServer();
    }


    @Override
    public void onResume() {
        super.onResume();
        if (aafn){
            initData();
        }
    }

    private void getDateFormServer() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message message = new Message();
                Map<String, String> mapParams = new HashMap<>();
                mapParams.put("pageNum", pageNum+"");
                mapParams.put("pageSize", 10 + "");
                String result = HttpUtils.httpPostString(Constants.API_ROOT + "/user/activity/list",
                        mapParams, getActivity());
                if (StringUtils.isBlank(result)) {
                    message.what = DATA_FAIL;
                } else {
                    message.what = DATA_SCUESSFUL;
                    ActivityList data =gson.fromJson(result, ActivityList.class);
                    message.obj = data;
                }
                postHandler.sendMessage(message);
            }
        }).start();

    }

    private void initView() {
        type = 1;
        listview1 = (ListView) view1.findViewById(R.id.listview);
        mToRefreshLayout = (PullToRefreshLayout) view1.findViewById(R.id.refresh_view);
        textview_data_null = (TextView) view1.findViewById(R.id.textview_data_null);
        mToRefreshLayout.setOnRefreshListener(new MyListener());
    }

    private class MyListener implements PullToRefreshLayout.OnRefreshListener {

        @Override
        public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
            retype = 0;
            pageNum = 1;
//            showShortToast1("PullToRefreshLayout");
            getDateFormServer();
        }

        @Override
        public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
            // 加载操作
            if (pageNum* 10 < totalCount) {
                // 千万别忘了告诉控件加载完毕了哦！
                retype = 1;
                pageNum ++;
                getDateFormServer();

            } else {
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.NODATE1);

            }
        }
    }

    Handler postHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DATA_FAIL:
                    showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                    break;
                case DATA_SCUESSFUL:
                    ActivityList response = (ActivityList) msg.obj;
                    aafn = false;
                    if (response != null) {
                        if (response.getCode().equals("0")) {
                            try{
                                totalCount = response.getData().getTotalCount();
                                List<ActivityList.DataBean.CrmActivityListBean>  itemList
                                        =response.getData().getCrmActivityList();
                                if (totalCount==0){
                                    textview_data_null.setVisibility(View.VISIBLE);
                                }else {
                                    textview_data_null.setVisibility(View.INVISIBLE);
                                }
                                if (!loading) {
                                    loading = true;
                                    activityAdapter = new ActivityAdapter(getActivity());
                                    activityAdapter.setData(itemList);
                                    listview1.setAdapter(activityAdapter);
                                }
                                else{
                                    if(retype==0){
                                        pageNum=1;
                                        activityAdapter.cleadate();
                                        activityAdapter.setData(itemList);
                                        activityAdapter.notifyDataSetChanged();
                                        listview1.setSelection(0);
                                        mToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                                    }
                                    else{
                                        if(pageNum*10<totalCount){
                                            pageNum++;
                                        }
                                        activityAdapter.setData(itemList);
                                        activityAdapter.notifyDataSetChanged();
//                                        listview1.setSelection(itemList.size() - 1);
                                        mToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                                    }

                                }
                            }catch (Exception e){

                            }
                        }
                        else{
                            showShortToast1(response.getMsg());
                        }
                    }
                    break;
            }

        }
    };
}
